package cc.comac.mom.datahandlers.entity.config;

import cc.comac.mom.bean.config.EquipConnectConfigParamBean;
import cc.comac.mom.bean.config.EquipConnectParamParamBean;
import cc.comac.mom.dao.entity.config.EquipConnectConfigDao;
import cc.comac.mom.dao.entity.config.EquipConnectParamDao;
import cc.comac.mom.entity.config.EquipConnectConfigEntity;
import cc.comac.mom.entity.config.EquipConnectParamEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class EquipConnectParamDataHandler extends ServiceImpl<EquipConnectParamDao, EquipConnectParamEntity> {
	@Autowired
	private EquipConnectParamDao equipConnectParamDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public ResultBean saveEquipConnectParam(EquipConnectParamEntity obj) {
		ResultBean resultBean = new ResultBean(true);
		// 校验主数据重复性
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("param_key", obj.getParamKey());
		map.put("param_item_code",obj.getParamItemCode());
		map.put("row_status", 1);
		List<EquipConnectParamEntity> selectByMap = equipConnectParamDao.selectByMap(map);
		if (selectByMap.size() > 0) {
			throw new RuntimeException("同一个参数组下" + obj.getParamItemCode() + "已经存在");
		}
		baseEntityDataUtil.setBaseEntityData(obj);
		int insert = equipConnectParamDao.insert(obj);
		if (insert < 1) {
			throw new RuntimeException("新增失败");
		}
		resultBean.setObject(obj);
		return resultBean;
	}

	public EquipConnectParamEntity getById(long id) {
		EquipConnectParamEntity bean = equipConnectParamDao.selectById(id);
		return bean;
	}

	public List<EquipConnectParamEntity>  selectByParamKey(String obj) {
		QueryWrapper<EquipConnectParamEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("param_key",obj);
		queryWrapper.orderByDesc("creation_time");
		List<EquipConnectParamEntity> list=equipConnectParamDao.selectList(queryWrapper);
		return list;
	}
}
